package com.ymatou.iapi.productmanager.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.UpdateSellerPorductTagsBean;
import com.ymatou.iapi.productmanager.service.UpdateSellerPorductTagsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.utils.TestDataManager;
/**
*修改营销标签
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UpdateSellerPorductTags{
	private static UpdateSellerPorductTagsBean updatesellerporducttagsBean;
	private static UpdateSellerPorductTagsCall updatesellerporducttagsCall;
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改营销标签");
	}

	@Before
	public void caseUp() {
		updatesellerporducttagsBean=new UpdateSellerPorductTagsBean();
		updatesellerporducttagsCall=new UpdateSellerPorductTagsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updatesellerporducttagsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateSellerPorductTags_001() {
		Logger.start(true,"修改营销标签成功");
		try {
			Integer sellerId=1;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			String tag="T"+TestDataManager.getRandomNum(4);
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			int tagId=ProductManagerCallServiceV3.AddSellerPorductTags(sellerId, cid, thirdcategoryid, tag);
			updatesellerporducttagsBean.setTagId(tagId);
			updatesellerporducttagsBean.setSellerId(sellerId);
			updatesellerporducttagsBean.setCategoryId(cid);
			updatesellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			updatesellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(3));
			updatesellerporducttagsCall.setData(updatesellerporducttagsBean);
			updatesellerporducttagsCall.callService();			
			Logger.verifyEquals("200", updatesellerporducttagsCall.getString("Code"), "验证code");
			int id=updatesellerporducttagsCall.getJsonObject().get("Data").getAsJsonObject().get("tag").getAsJsonObject().get("id").getAsInt();
			List<Map> list = intergratedProductMapper.selectYmtSellerProductTag(updatesellerporducttagsBean.getSellerId(), updatesellerporducttagsBean.getThirdcategoryId());
			Map m = list.stream().filter(x->{return x.get("Tag").toString().equals(updatesellerporducttagsBean.getTags());}).findAny().get();
			Logger.debug(m);
			Logger.verifyNotNull(m, "营销标签查询成功");
			Logger.verifyEquals(updatesellerporducttagsBean.getSellerId(), m.get("SellerId"), "验证sellerid");
			Logger.verifyEquals(updatesellerporducttagsBean.getTags(), m.get("Tag"), "验证Tag");
			Logger.verifyEquals(updatesellerporducttagsBean.getCategoryId(), m.get("CategoryId"), "验证CategoryId");
			Logger.verifyEquals(updatesellerporducttagsBean.getThirdcategoryId(), m.get("ThirdcategoryId"), "验证ThirdcategoryId");
			Logger.verifyEquals(0, m.get("Status"), "验证Status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UpdateSellerPorductTags_002() {
		Logger.start(true,"不输入sellerid失败");
		try {
			Integer sellerId=1;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			String tag="T"+TestDataManager.getRandomNum(4);
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			int tagId=ProductManagerCallServiceV3.AddSellerPorductTags(sellerId, cid, thirdcategoryid, tag);
			updatesellerporducttagsBean.setTagId(tagId);
//			updatesellerporducttagsBean.setSellerId(sellerId);
			updatesellerporducttagsBean.setCategoryId(cid);
			updatesellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			updatesellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(3));
			updatesellerporducttagsCall.setData(updatesellerporducttagsBean);
			updatesellerporducttagsCall.callService();			
			Logger.verifyEquals("400", updatesellerporducttagsCall.getString("Code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UpdateSellerPorductTags_003() {
		Logger.start(true,"不输入categoryId失败");
		try {
			Integer sellerId=1;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			String tag="T"+TestDataManager.getRandomNum(4);
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			int tagId=ProductManagerCallServiceV3.AddSellerPorductTags(sellerId, cid, thirdcategoryid, tag);
			updatesellerporducttagsBean.setTagId(tagId);
			updatesellerporducttagsBean.setSellerId(sellerId);
//			updatesellerporducttagsBean.setCategoryId(cid);
			updatesellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			updatesellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(3));
			updatesellerporducttagsCall.setData(updatesellerporducttagsBean);
			updatesellerporducttagsCall.callService();			
			Logger.verifyEquals("400", updatesellerporducttagsCall.getString("Code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UpdateSellerPorductTags_004() {
		Logger.start(true,"不输入thirdcategoryId失败");
		try {
			Integer sellerId=1;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			String tag="T"+TestDataManager.getRandomNum(4);
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			int tagId=ProductManagerCallServiceV3.AddSellerPorductTags(sellerId, cid, thirdcategoryid, tag);
			updatesellerporducttagsBean.setTagId(tagId);
			updatesellerporducttagsBean.setSellerId(sellerId);
			updatesellerporducttagsBean.setCategoryId(cid);
//			updatesellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			updatesellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(3));
			updatesellerporducttagsCall.setData(updatesellerporducttagsBean);
			updatesellerporducttagsCall.callService();			
			Logger.verifyEquals("400", updatesellerporducttagsCall.getString("Code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UpdateSellerPorductTags_005() {
		Logger.start(true,"不输入tags失败");
		try {
			Integer sellerId=1;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			String tag="T"+TestDataManager.getRandomNum(4);
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			int tagId=ProductManagerCallServiceV3.AddSellerPorductTags(sellerId, cid, thirdcategoryid, tag);
			updatesellerporducttagsBean.setTagId(tagId);
			updatesellerporducttagsBean.setSellerId(sellerId);
			updatesellerporducttagsBean.setCategoryId(cid);
			updatesellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
//			updatesellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(3));
			updatesellerporducttagsCall.setData(updatesellerporducttagsBean);
			updatesellerporducttagsCall.callService();			
			Logger.verifyEquals("400", updatesellerporducttagsCall.getString("Code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UpdateSellerPorductTags_006() {
		Logger.start(true,"买手三级类目营销标签不能超过20个");
		try {
			Integer sellerId=1;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			String tag="T"+TestDataManager.getRandomNum(3);
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			int tagId=ProductManagerCallServiceV3.AddSellerPorductTags(sellerId, cid, thirdcategoryid+1, tag);
			for(int i=0;i<20;i++) {
				ProductManagerCallServiceV3.AddSellerPorductTags(sellerId, cid, thirdcategoryid, tag+i);
			}
			updatesellerporducttagsBean.setTagId(tagId);
			updatesellerporducttagsBean.setSellerId(sellerId);
			updatesellerporducttagsBean.setCategoryId(cid);
			updatesellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			updatesellerporducttagsBean.setTags("T"+TestDataManager.getRandomNum(5));
			updatesellerporducttagsCall.setData(updatesellerporducttagsBean);
			updatesellerporducttagsCall.callService();			
			Logger.verifyEquals("201", updatesellerporducttagsCall.getString("Code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UpdateSellerPorductTags_007() {
		Logger.start(true,"已存在相同标签失败");
		try {
			Integer sellerId=7;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			String tag="T"+TestDataManager.getRandomNum(4);
			String tag2="T"+TestDataManager.getRandomNum(4);
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			int tagId=ProductManagerCallServiceV3.AddSellerPorductTags(sellerId, cid, thirdcategoryid, tag);
			int tagId2=ProductManagerCallServiceV3.AddSellerPorductTags(sellerId, cid, thirdcategoryid, tag2);
			updatesellerporducttagsBean.setTagId(tagId);
			updatesellerporducttagsBean.setSellerId(sellerId);
			updatesellerporducttagsBean.setCategoryId(cid);
			updatesellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			updatesellerporducttagsBean.setTags(tag2);
			updatesellerporducttagsCall.setData(updatesellerporducttagsBean);
			updatesellerporducttagsCall.callService();			
			Logger.verifyEquals("201", updatesellerporducttagsCall.getString("Code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UpdateSellerPorductTags_008() {
		Logger.start(true,"标签长度超出12个字符失败");
		try {
			Integer sellerId=1;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			String tag="T"+TestDataManager.getRandomNum(4);
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			int tagId=ProductManagerCallServiceV3.AddSellerPorductTags(sellerId, cid, thirdcategoryid, tag);
			updatesellerporducttagsBean.setTagId(tagId);
			updatesellerporducttagsBean.setSellerId(sellerId);
			updatesellerporducttagsBean.setCategoryId(cid);
			updatesellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			updatesellerporducttagsBean.setTags("T"+TestDataManager.getRandomNum(12));
			updatesellerporducttagsCall.setData(updatesellerporducttagsBean);
			updatesellerporducttagsCall.callService();			
			Logger.verifyEquals("400", updatesellerporducttagsCall.getString("Code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UpdateSellerPorductTags_009() {
		Logger.start(true,"tagid和sellerid不一致修改失败");
		try {
			Integer sellerId=1;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			String tag="T"+TestDataManager.getRandomNum(4);
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			int tagId=ProductManagerCallServiceV3.AddSellerPorductTags(sellerId, cid, thirdcategoryid, tag);
			updatesellerporducttagsBean.setTagId(tagId);
			updatesellerporducttagsBean.setSellerId(sellerId+2);
			updatesellerporducttagsBean.setCategoryId(cid);
			updatesellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			updatesellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(3));
			updatesellerporducttagsCall.setData(updatesellerporducttagsBean);
			updatesellerporducttagsCall.callService();			
			Logger.verifyEquals("201", updatesellerporducttagsCall.getString("Code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateSellerPorductTags_010() {
		Logger.start(true,"修改营销标签不能改类目");
		try {
			Integer sellerId=1;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			String tag="T"+TestDataManager.getRandomNum(4);
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			int tagId=ProductManagerCallServiceV3.AddSellerPorductTags(sellerId, cid, thirdcategoryid, tag);
			updatesellerporducttagsBean.setTagId(tagId);
			updatesellerporducttagsBean.setSellerId(sellerId);
			updatesellerporducttagsBean.setCategoryId(cid+1);
			updatesellerporducttagsBean.setThirdcategoryId(thirdcategoryid+1);
			updatesellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(3));
			updatesellerporducttagsCall.setData(updatesellerporducttagsBean);
			updatesellerporducttagsCall.callService();			
			Logger.verifyEquals("200", updatesellerporducttagsCall.getString("Code"), "验证code");
			int id=updatesellerporducttagsCall.getJsonObject().get("Data").getAsJsonObject().get("tag").getAsJsonObject().get("id").getAsInt();
			List<Map> list = intergratedProductMapper.selectYmtSellerProductTag(updatesellerporducttagsBean.getSellerId(), thirdcategoryid);
			Map m = list.stream().filter(x->{return x.get("Tag").toString().equals(updatesellerporducttagsBean.getTags());}).findAny().get();
			Logger.debug(m);
			Logger.verifyNotNull(m, "营销标签查询成功");
			//现在的逻辑是 根据id修改tag 不会变化类目
			Logger.verifyEquals(updatesellerporducttagsBean.getSellerId(), m.get("SellerId"), "验证sellerid");
			Logger.verifyEquals(updatesellerporducttagsBean.getTags(), m.get("Tag"), "验证Tag");
			Logger.verifyEquals(cid, m.get("CategoryId"), "验证CategoryId");
			Logger.verifyEquals(thirdcategoryid, m.get("ThirdcategoryId"), "验证ThirdcategoryId");
			Logger.verifyEquals(0, m.get("Status"), "验证Status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}